package beanAction;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import modele.beanEntity.User;
import modele.mapper.UserMapper;
@ManagedBean(name="usermanagerbean")
@ViewScoped
public class UserManagerBean implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -9062344637257625620L;
	private List<User> userList;
	private User currentUser;
	private UIComponent uiComponent;
	
	public UserManagerBean(){
		UserMapper um;
		try {
			um = new UserMapper();
			userList = um.getUserList();
			currentUser = getFirstUser();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private User getFirstUser(){
		if(!userList.isEmpty()){
			return userList.get(0);
		}
		return new User();
	}
	
	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(User currentUser) {
		if(currentUser == null){
			this.currentUser = new User();
		}else{
			this.currentUser = currentUser;
		}
	}
	
	public void attrListener(ActionEvent e){
		uiComponent= e.getComponent();
	}
	
	
	public void saveUser(){
		if(currentUser != null){
			UserMapper um;
			try {
				um = new UserMapper();
				if(um.userExists(currentUser.getId())){
					if(um.updateUser(currentUser)){
						userList = um.getUserList();
						LoginBean lb = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginbean");
						if(currentUser.getId() == lb.getUser().getId()){
							lb.setLogin(currentUser.getLogin());
							lb.setPassword(currentUser.getPassword());
							lb.connectAdmin();
						}
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"Update success !",null));
					}else{
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Update failure !",null));
					}
				}else{
					if(um.createUser(currentUser)){
						userList = um.getUserList();
						currentUser = getFirstUser();
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"User added successfully !",null));
					}else{
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"Adding failed !",null));
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Update failure !",null));
				e.printStackTrace();
			}
			
		}
	}
	
	public void delete(){
		LoginBean lb = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginbean");
		if(currentUser.getId() == lb.getUser().getId()){
			FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"You can't delete your own account !",null));
		}else{
			UserMapper um;
			try {
				um = new UserMapper();
				if(um.deleteUser(currentUser)){
					userList = um.getUserList();
					currentUser = getFirstUser();
					FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"User deleted successfully!",null));
				}else{
					FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Deleting failed !",null));
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Deleting failed !",null));
				e.printStackTrace();
			}
		}
	}
	
}
